package performancetest;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class LaUDVSMysql {

	/**
	 * @param args
	 * @throws ClassNotFoundException 
	 * @throws SQLException 
	 */
	static int NUMBER=100000;
	public static void main(String[] args) throws ClassNotFoundException, SQLException {
		String host=System.getProperty("url","jdbc:laud://166.111.132.227:9170?data_port=9091&support_function=false&hive=false");
		Class.forName("cn.edu.thu.laud.jdbc.LaUDDriver");
		Connection con = DriverManager.getConnection(host, "", "");
		Statement statement=con.createStatement();
		try{
			statement.execute("drop keyspace  hxd_test_vs_mysql ;");
		}catch (SQLException e) {
			System.err.println(e.getMessage());
		}
		try{
			statement.execute("create keyspace  hxd_test_vs_mysql with  strategy_class ='org.apache.cassandra.locator.SimpleStrategy' and strategy_options:replication_factor=1;");
		}catch (SQLException e) {
			System.err.println(e.getMessage());
		}
		statement.execute("use hxd_test_vs_mysql");
		try{
			statement.execute("create columnfamily cf1 (id varchar primary key,name varchar,value int)");
		}catch (SQLException e) {
			System.err.println(e.getMessage());
		}
		long start=System.nanoTime();
		for(int i=0;i<NUMBER;i++){
			statement.execute(String.format("insert into cf1(id,name,value) values (%d,%s,%d)",i,"hxd"+i,i*10)); //,'F:/lib/laud-thrift-1.0.7-1.jar'
			if(i%1000==0){
				if(i%100000==0){
					System.out.println();
				}
				System.out.print(".");
			}
		}
		long end=System.nanoTime();
		System.out.println("laud cost time:"+(end-start));
		try{
			con.close();
		}catch(SQLException e){
			System.err.println(e.getMessage());
		}
		//then mysql
		Class.forName("com.mysql.jdbc.Driver");
		Connection connect = DriverManager.getConnection("jdbc:mysql://166.111.132.227:3306/test","hxd", "571219hxd");
		statement = connect.createStatement();
		try{
			statement.execute("drop database  hxd_test_vs_mysql ;");
		}catch (SQLException e) {
			System.err.println(e.getMessage());
		}
		try{
			statement.execute("create database  hxd_test_vs_mysql ;");
		}catch (SQLException e) {
			System.err.println(e.getMessage());
		}
		statement.execute("use hxd_test_vs_mysql");
		try{
			statement.execute("create table cf1 (id varchar(100) primary key,name varchar(100),value int)");
		}catch (SQLException e) {
			System.err.println(e.getMessage());
		}
		start=System.nanoTime();
		for(int i=0;i<NUMBER;i++){
			statement.execute(String.format("insert into cf1(id,name,value) values ('%d','%s',%d)",i,"hxd"+i,i*10)); //,'F:/lib/laud-thrift-1.0.7-1.jar'
			if(i%1000==0){
				if(i%100000==0){
					System.out.println();
				}
				System.out.print(".");
			}
		}
		end=System.nanoTime();
		System.out.println("mysql cost time:"+(end-start));
		try{
			con.close();
		}catch(SQLException e){
			System.err.println(e.getMessage());
		}

	}

}
